package cloud.xuxiaowei.oauth2.handler;

import cloud.xuxiaowei.utils.Response;
import cloud.xuxiaowei.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 资源访问拒绝 处理程序
 *
 * @author xuxiaowei
 * @since 0.0.1
 */
@Slf4j
public class ResourceAccessDeniedHandler implements AccessDeniedHandler {

	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response,
			AccessDeniedException accessDeniedException) throws IOException, ServletException {

		log.error("资源访问拒绝 处理程序：", accessDeniedException);

		Response<?> error = Response.error(HttpStatus.FORBIDDEN.value(), accessDeniedException.getMessage());

		ResponseUtils.response(response, error);

	}

}
